using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace LANamp
{
    public partial class frmSelect : Form
    {
        public frmSelect(ArrayList items)
        {
            this.items = (ArrayList)items.Clone();
            InitializeComponent();
        }

        private ArrayList items;
        private bool selectingInList = false;
        private System.Timers.Timer updateTimer;
        public delegate void ItemsSelectedEvent(MusicInfo Selection);
        public event ItemsSelectedEvent ItemSelectedAsLast;
        public event ItemsSelectedEvent ItemSelectedAsFirst;

        private void frmSelect_Load(object sender, EventArgs e)
        {
            updateTimer = new System.Timers.Timer();
            updateTimer.Interval = 300;
            updateTimer.AutoReset = false;
            updateTimer.Elapsed += new System.Timers.ElapsedEventHandler(updateTimer_Elapsed);
            UpdateItems();
        }

        void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lbItems.Invoke(new MethodInvoker(UpdateItems));
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            updateTimer.Stop();
            updateTimer.Start();
        }

        private void UpdateItems()
        {
            MusicInfo currentSelected = (MusicInfo)lbItems.SelectedItem;
            lbItems.BeginUpdate();
            lbItems.Items.Clear();
            String[] needs = tbFilter.Text.Split(new char[1] { ' ' });
            bool failedsingle;
            bool failed;
            foreach (object item in items)
            {
                failed = false;
                foreach (string need in needs)
                {
                    failedsingle = true;
                    if (((MusicInfo)item).Song != null)
                    {
                        if (((MusicInfo)item).Song.IndexOf(need,StringComparison.OrdinalIgnoreCase) > -1)
                        {
                            failedsingle = false;
                        }
                    }
                    if (((MusicInfo)item).Filepath.IndexOf(need, StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        failedsingle = false;
                    }
                    if (failedsingle)
                    {
                        failed = true;
                        break;
                    }
                }
                if (!failed)
                {
                    lbItems.Items.Add(item);
                }
            }
            if (lbItems.Items.Count > 0) lbItems.SelectedIndex = 0;
            lbItems.EndUpdate();
            btnAddFirst.Enabled = btnAddLast.Enabled = (lbItems.Items.Count > 0);
            int matchCount = lbItems.Items.Count;
            this.Text = "Search... ("+matchCount+" match"+((matchCount != 1) ? "es":"")+")";
            lbItems.SelectedItem = currentSelected;
        }

        private void lbItems_DoubleClick(object sender, EventArgs e)
        {
            if (lbItems.SelectedIndex > -1)
            {
                MusicInfo song = (MusicInfo)lbItems.SelectedItem;
                MusicInfo next = null;
                if (lbItems.SelectedIndex < (lbItems.Items.Count - 1))
                    next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex + 1];
                else
                    if (lbItems.SelectedIndex > 0)
                        next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex - 1];
                this.items.Remove(song);
                if (ItemSelectedAsLast != null) ItemSelectedAsLast(song);
                UpdateItems();
                if (next != null) lbItems.SelectedItem = next;
            }
        }

        private void btnAddLast_Click(object sender, EventArgs e)
        {
            if (lbItems.SelectedIndex > -1)
            {
                MusicInfo song = (MusicInfo)lbItems.SelectedItem;
                MusicInfo next = null;
                if (lbItems.SelectedIndex < (lbItems.Items.Count - 1))
                    next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex + 1];
                else
                    if (lbItems.SelectedIndex > 0)
                        next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex - 1];
                this.items.Remove(song);
                Console.Out.WriteLine(song.Filepath);
                if (ItemSelectedAsLast != null) ItemSelectedAsLast(song);
                UpdateItems();
                if (next != null) lbItems.SelectedItem = next;
            }
        }

        private void btnAddFirst_Click(object sender, EventArgs e)
        {
            if (lbItems.SelectedIndex > -1)
            {
                MusicInfo song = (MusicInfo)lbItems.SelectedItem;
                MusicInfo next = null;
                if (lbItems.SelectedIndex < (lbItems.Items.Count - 1))
                    next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex + 1];
                else
                    if (lbItems.SelectedIndex > 0)
                        next = (MusicInfo)lbItems.Items[lbItems.SelectedIndex - 1];
                this.items.Remove(song);
                if (ItemSelectedAsFirst != null) ItemSelectedAsFirst(song);
                UpdateItems();
                if (next != null) lbItems.SelectedItem = next;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmSelect_FormClosing(object sender, FormClosingEventArgs e)
        {
            updateTimer.Stop();
            updateTimer.Close();
        }

        private void frmSelect_Shown(object sender, EventArgs e)
        {
            tbFilter.Focus();
        }

        private void frmSelect_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();
            if (e.KeyCode == Keys.Enter)
            {
                if (lbItems.SelectedIndex == -1)
                {
                    lbItems.SelectedIndex = 0;
                }
                lbItems_DoubleClick(sender, e);
                if (!selectingInList) this.Close();
            }
        }

        private void lbItems_KeyUp(object sender, KeyEventArgs e)
        {
            if (lbItems.Items.Count > 0)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (lbItems.SelectedIndex == -1)
                    {
                        lbItems.SelectedIndex = 0;
                    }
                    lbItems_DoubleClick(sender, e);
                    this.Close();
                }
            }           
        }

        private void tbFilter_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (lbItems.SelectedIndex < (lbItems.Items.Count - 1))
                {
                    lbItems.SelectedIndex++;
                }
                selectingInList = true;
            }
            if (e.KeyCode == Keys.Up)
            {
                if (lbItems.SelectedIndex > 0)
                {
                    lbItems.SelectedIndex--;
                    selectingInList = true;
                }
            }

        }



    }
}